package com.zj.leetcode.easy.tree;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: algorithm
 * @description: 二叉树的后序遍历
 * 145 binary-tree-postorder-traversal
 * @author: Zhang Bo
 * @create: 2022-01-14 17:05
 **/
public class BinaryTreePostorderTraversal {
    public static void main(String[] args) {
        Solution solution = new BinaryTreePostorderTraversal().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)


    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    class Solution {
        List<Integer> list = new ArrayList<>();

        public List<Integer> postorderTraversal(TreeNode root) {
            if (root == null) {
                return list;
            }

            this.dfs(root);

            return list;

        }

        private void dfs(TreeNode node) {
            if (node != null) {
                if (node.left != null) {
                    this.dfs(node.left);
                }

                if (node.right != null) {
                    this.dfs(node.right);
                }
                list.add(node.val);

            }
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}

